package com.bolang.onlycode.demo.MyThread;

import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LoopABClock {
    private int num;
    private Lock lock =new ReentrantLock();
    private void print(int targetNum){
        for (int i = 0; i < 3;) {
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"线程第"+i+"次进来了");
                if(num % 3==targetNum){
                    num++;
                    i++;
                    System.out.print(Thread.currentThread().getName());
                }
            }finally {
                lock.unlock();
            }

        }
    }
    public static void main(String[] args) {
        LoopABClock loopABC = new LoopABClock();
        ConcurrentLinkedQueue<Integer> integers = new ConcurrentLinkedQueue<>();
        new Thread(() -> loopABC.print(0) , "a").start();
        new Thread(() ->loopABC.print(1) , "a").start();
        new Thread(() ->loopABC.print(2) , "a").start();
//        new Thread(() ->integers.add(1) , "a").start();
//        new Thread(() ->integers.add(2) , "a").start();
//        new Thread(() ->integers.add(3) , "a").start();

    }

}
